/*
 * CPersonFlowCounter.h
 *
 *  Created on: 2018年10月3日
 *      Author: zhengboyuan
 */

#ifndef CPERSONFLOWCOUNTER_H_
#define CPERSONFLOWCOUNTER_H_

#include "PersonFlowCounter.h"
#include "TMap.h"
#include "SharedPtr.h"
#include "CameraDevice.h"
#include "DataStore.h"

typedef std::shared_ptr< CameraDevice >     CameraDevicePtr;
typedef comn::Map< std::string, CameraDevicePtr >   CameraDeviceMap;


class CPersonFlowCounter: public PersonFlowCounter, public PersonFlowSink
{
public:
	static void init();
	static void quit();

	static void CALLBACK ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser);

public:
    CPersonFlowCounter();
    virtual ~CPersonFlowCounter();

    virtual bool open(const char* filepath);

    virtual void close();

    virtual bool addCamera(const Camera& camera);

    virtual bool deleteCamera(const std::string& id);

    virtual void deleteCameras();

	virtual bool getCamera(const std::string& id, Camera& camera);

	virtual bool isConnected(const std::string& id);

    virtual void setSink(PersonFlowSink* sink);

    virtual bool getCount(const std::string& id, PersonFlowCount& flowCount);

    virtual void getTotalCount(PersonFlowCount& flowCount);


	virtual int getCameraCount();
	virtual bool getCamera(int index, Camera& camera);

	virtual bool reconnect(const std::string& id);

protected:
    virtual void onFlowCount(const std::string& id, const PersonFlowCount& flowCount);

    virtual void onDeviceStatus(const std::string& id, int connected);

protected:
    bool saveDevices();
    bool loadDevices();

	bool doAddCamera(const Camera& camera);

	void exceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle);

	bool findDevice(LONG userID, CameraDevicePtr& device);

    void saveFlow(const std::string& id, const PersonFlowCount& flowCount);

protected:
    CameraDeviceMap m_deviceMap;
    PersonFlowSink* m_sink;
    std::string m_filepath;

    DataStorePtr    m_dataStore;

};

#endif /* CPERSONFLOWCOUNTER_H_ */
